using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using Telerik.WinControls.Primitives;
using System.Data.SqlClient;
using System.Xml;
namespace checkConnect
{
    public partial class FrmStartProgram : Telerik.WinControls.RadForm
    {
        public FrmStartProgram()
        {
            InitializeComponent();
        }
        // load form
        private void FrmStartProgram_Load(object sender, EventArgs e)
        {
            lbHelper.Text = string.Empty;
            groupSever.Enabled = true;
            groupDatabase.Enabled = false;
            groupCreateDatabase.Enabled = false;

        }
        // close form
        private void FrmStartProgram_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Do you want to close form?", "Alert", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                e.Cancel = true;
            }
        }
        // check connec to sever
        private void btConnectToSever_Click(object sender, EventArgs e)
        {
            if (txtSeverName.Text.Trim() != string.Empty)
            {
                CheckConnection ckCnn = new CheckConnection();
                ckCnn.ServerName = txtSeverName.Text.Trim();
                ckCnn.CheckCnn();
                if (ckCnn.ResultServer)
                {
                    lbHelper.ForeColor = Color.Green;
                    lbHelper.Text = "Connect to Sever successful";
                    groupSever.Enabled = false;
                    groupDatabase.Enabled = true;
                    groupCreateDatabase.Enabled = false;

                    // save file App.config
                    string[] xmlConfig = new string[]{ 
                                       "<?xml version=\"1.0\" encoding=\"utf-8\" ?> ",
                                       "<configuration> ",
                                       "<connectionStrings> ",
                                       "<add name=\"STR_CONN_2\"  connectionString=\"Data Source=" + txtSeverName.Text.Trim() + ";Initial Catalog=" + txtDatabaseName.Text.Trim() + ";Integrated Security=True\" /> ",
                                       "</connectionStrings> ",
                                       "</configuration>"};
                    System.IO.FileStream fs = new System.IO.FileStream("App.config", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
                    // create a stream writer
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
                    // write to file (buffer), where textbox1 is your text box
                    for (int i = 0  ; i < 6; i++)
                    {
                        sw.WriteLine(xmlConfig[i]);    
                    }
                    // flush buffer (so the text really goes into the file)
                    sw.Flush();
                    // close stream writer and file
                    sw.Close(); 
                    fs.Close();

                    // check connect

                }
                else
                {
                    lbHelper.ForeColor = Color.Red;
                    lbHelper.Text = "Connect to Sever fail";
                    groupSever.Enabled = true;
                    groupDatabase.Enabled = false;
                    groupCreateDatabase.Enabled = false;
                }
            }
        }
        // check exist database
        private void btConnectToDatabase_Click(object sender, EventArgs e)
        {
            CheckConnection ckCnn = new CheckConnection();
            ckCnn.ServerName = txtSeverName.Text.Trim();
            ckCnn.Database = txtDatabaseName.Text.Trim();
            ckCnn.checkDatabase();
            if (ckCnn.ResultDB)
            {
                lbHelper.ForeColor = Color.Green;
                lbHelper.Text = "Database is exists";
                groupSever.Enabled = false;
                groupDatabase.Enabled = false;
                groupCreateDatabase.Enabled = true;
            }
            else
            {
                lbHelper.ForeColor = Color.Red;
                lbHelper.Text = "Database is not exists";
                groupSever.Enabled = false;
                groupDatabase.Enabled = false;
                groupCreateDatabase.Enabled = true;

                // create new database
                string cnnString = String.Format(@"Data Source={0};Integrated Security=true;Connect Timeout=2", ckCnn.ServerName);
                string sqlCreateDatabase = "create database " + txtDatabaseName.Text.Trim();
                int temp = ckCnn.executeNonQuery(cnnString, CommandType.Text, sqlCreateDatabase, null);
                lbHelper.ForeColor = Color.Green;
                lbHelper.Text = "create database successful";
            }
        }
        // browser file sql
        private void btBrowserFileSQL_Click(object sender, EventArgs e)
        {
            openFileSQL.Title = "Browser file sql";
            openFileSQL.FileName = string.Empty;
            openFileSQL.Filter = "JPeg Image|*.SQL";
            if (openFileSQL.ShowDialog() == DialogResult.OK)
            {
                if (openFileSQL.FileName.Length > 0)
                {
                    txtLinkFileSQL.Text = openFileSQL.FileName.Trim();
                    btStartCreateDatabase.Enabled = true;
                }
            }
        }
        private void txtLinkFileSQL_Click(object sender, EventArgs e)
        {
            btBrowserFileSQL_Click(null, null);
        }
        // create database
        private void btStartCreateDatabase_Click(object sender, EventArgs e)
        {
            // create table and database
            if (txtLinkFileSQL.Text.Trim().Length > 0)
            {
                // check exist database
                CheckConnection ckCnn = new CheckConnection();
                ckCnn.ServerName = txtSeverName.Text.Trim();
                ckCnn.Database = txtDatabaseName.Text.Trim();
                ckCnn.checkDatabase();
                if (ckCnn.ResultDB)
                {
                    string cnnString = String.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=true", ckCnn.ServerName, ckCnn.Database);
                    string imgPath = txtLinkFileSQL.Text.Trim();
                    FileInfo file = new FileInfo(imgPath);
                    string script = file.OpenText().ReadToEnd();
                    int result = ckCnn.executeNonQuery(cnnString, CommandType.Text, script, null);    
                }
            }
        }
        
    }
}
